/*
 *
 * This file is part of Aiphial.
 *
 * Copyright (c) 2010 Nicolay Mitropolsky <NicolayMitropolsky@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * ClusterPanel.java
 *
 * Created on 21.08.2009, 13:27:02
 */
package MyImage;

import MyImage.utls.ClustersMap;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.List;
import me.uits.aiphial.general.basic.Cluster;
import me.uits.aiphial.imaging.LuvPoint;

/**
 *
 * @author Nicolay Mitropolsky <NicolayMitropolsky@gmail.com>
 */
public class ClusterPanel1 extends PicturePanel
{

    int x;
    int y;

    public ClusterPanel1()
    {
        initComponents();

        final ClusterPanel1 panel = this;

        this.addMouseListener(new MouseAdapter()
        {

            @Override
            public void mouseReleased(MouseEvent e)
            {
                BufferedImage i = getImage();

                x = e.getX() * i.getWidth() / getWidth();
                y = e.getY() * i.getHeight() / getHeight();




//
//                BufferedImage bm = new BufferedImage(getImage().getColorModel(), getImage().copyData(null), false, null);
//                Cluster cluster = clustersMap.getAt(x, y);
//                BufferedImage cur = getImage();
//                for (LuvPoint point : cluster)
//                {
//                    cur.setRGB(point.getX().intValue(), point.getY().intValue(), Color.CYAN.getRGB());
//
//                }
//                for (LuvPoint point : clustersMap.get8Boundary(cluster))
//                {
//                    cur.setRGB(point.getX().intValue(), point.getY().intValue(), Color.WHITE.getRGB());
//                }
//                panel.setImage(cur);



                panel.repaint();

            }
        });
    }
    private List<Cluster<LuvPoint>> clusters;
    private ClustersMap clustersMap;

    private int corectW(float w)
    {
        return (int) (w * getWidth() / getImage().getWidth());
    }

    private int corectH(float h)
    {
        return (int) (h * getHeight() / getImage().getHeight());
    }

    public void paintClusters(Graphics g)
    {

        Cluster<LuvPoint> cluster = clustersMap.getAt(x, y);
        

        Color oldc = g.getColor();
        g.setColor(Color.CYAN);
        for (LuvPoint point : cluster)
        {
            g.fillOval(corectW(point.getX()), corectH(point.getY()), getWidth() / getImage().getWidth(), getHeight() / getImage().getHeight());

        }

        g.setColor(oldc);

        Polygon polygon = new Polygon();
        for (LuvPoint point : clustersMap.getOrderedBoundary(cluster))
        {

            //g.fillOval(corectW(point.getX()), corectH(point.getY()), getWidth() / getImage().getWidth(), getHeight() / getImage().getHeight());

            polygon.addPoint(corectW(point.getX()), corectH(point.getY()));
        }

        // polygon.invalidate();

        g.drawPolygon(polygon);

    }

    @Override
    public void paint(Graphics g)
    {
        //super.paint(g);
        //Graphics2D g2d = (Graphics2D)g;
        Color old = g.getColor();
        g.setColor(this.getBackground());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(old);

        paintClusters(g);
        // Рисуем подкомпоненты.
        super.paintChildren(g);
        // Рисуем рамку
        super.paintBorder(g);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @return the clusters
     */
    public List<Cluster<LuvPoint>> getClusters()
    {
        return clusters;
    }

    /**
     * @param clusters the clusters to set
     */
    public void setClusters(List<Cluster<LuvPoint>> clusters)
    {
        clustersMap = new ClustersMap(getImage().getWidth(), getImage().getHeight());
        this.clusters = clusters;
        clustersMap.buildMap(clusters);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
